//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.model.vo
{
	import __AS3__.vec.Vector;
	
	public class ServiceVO
	{
		//----------------------------------------------------------------------------
		//
		// Constance of service type
		//
		//----------------------------------------------------------------------------
		
		public static const SERVICE_OTHER		:int = 0;
		public static const SERVICE_METAWEBLOG	:int = 1;
		public static const SERVICE_ATOM		:int = 2;
		
		public static const OTHER_MYSPACE_CN	:String = "Myspace.cn";
		public static const OTHER_MYSPACE_COM	:String = "Myspace.com";
		public static const OTHER_FLICKR		:String = "Flickr.com";
		public static const OTHER_FACEBOOK		:String = "Facebook.com";
		
		public static const CONFIG_LIVE			:String = "livespace";
		public static const CONFIG_QQ			:String = "qzone";
		public static const AUTH_LIVE			:String = "MyBlog";
		
		//----------------------------------------------------------------------------
		//
		// Properties
		//
		//----------------------------------------------------------------------------
		
		public var login_width	:int = 640;
		public var login_height	:int = 480;
		
		private var _sid:int = -1;
		public function get sid():int
		{
			return _sid;
		}
		public function set sid(value:int):void
		{
			_sid = value;
		}
		
		private var _userId:int = -1;
		public function get userId():int
		{
			return _userId;
		}
		public function set userId(value:int):void
		{
			_userId = value;
		}
		
		private var _type:int = -1;
		public function get type():int
		{
			return _type;
		}
		public function set type(value:int):void
		{
			_type = value;
		}
		
		private var _postid:int = -1;
		public function get postid():int
		{
			return _postid;
		}
		public function set postid(value:int):void
		{
			_postid = value;
		}
		
		
		[Bindable]
		private var _name:String;
		public function get name():String
		{
			return _name;
		}
		public function set name(value:String):void
		{
			_name = value;
		}
		
		private var _link:String;
		public function get link():String
		{
			return _link;
		}
		public function set link(value:String):void
		{
			_link = value;
		}
		
		private var _key:String;
		public function get key():String
		{
			return _key;
		}
		public function set key(value:String):void
		{
			_key = value;
		}
		
		private var _secret:String;
		public function get secret():String
		{
			return _secret;
		}
		public function set secret(value:String):void
		{
			_secret = value;
		}
		
		private var _username:String;
		public function get username():String
		{
			return _username;
		}
		public function set username(value:String):void
		{
			_username = value;
		}
		
		private var _password:String;
		public function get password():String
		{
			return _password;
		}
		public function set password(value:String):void
		{
			_password = value;
		}
		
		private var _authhead:String;
		public function get authhead():String
		{
			return _authhead;
		}
		public function set authhead(value:String):void
		{
			_authhead = value;
		}
		
		private var _authparams:String;
		public function get authparams():String
		{
			return _authparams;
		}
		public function set authparams(value:String):void
		{
			_authparams = value;
		}
		
		private var _login_url:String;
		public function get login_url():String
		{
			return _login_url;
		}
		public function set login_url(value:String):void
		{
			_login_url = value;
		}
		
		private var _login_flag:String;
		public function get login_flag():String
		{
			return _login_flag;
		}
		public function set login_flag(value:String):void
		{
			_login_flag = value;
		}
		
		private var _endpoint:String;
		public function get endpoint():String
		{
			return _endpoint;
		}
		public function set endpoint(value:String):void
		{
			_endpoint = value;
		}
		
		private var _destination:String;
		public function get destination():String
		{
			return _destination;
		}
		public function set destination(value:String):void
		{
			_destination = value;
		}
		
		private var _config:String;
		public function get config():String
		{
			return _config;
		}
		public function set config(value:String):void
		{
			_config = value;
		}
		
		private var _needAlert:Boolean;
		public function get needAlert():Boolean
		{
			return _needAlert;
		}
		public function set needAlert(value:Boolean):void
		{
			_needAlert = value;
		}
		
		private var _isGetAlbum:Boolean;
		public function get isGetAlbum():Boolean
		{
			return _isGetAlbum;
		}
		public function set isGetAlbum(value:Boolean):void
		{
			_isGetAlbum = value;
		}
		
		private var _isPostAlbum:Boolean;
		public function get isPostAlbum():Boolean
		{
			return _isPostAlbum;
		}
		public function set isPostAlbum(value:Boolean):void
		{
			_isPostAlbum = value;
		}
		
		private var _isPostBlog:Boolean;
		public function get isPostBlog():Boolean
		{
			return _isPostBlog;
		}
		public function set isPostBlog(value:Boolean):void
		{
			_isPostBlog = value;
		}
		
		private var _isEditBlog:Boolean;
		public function get isEditBlog():Boolean
		{
			return _isEditBlog;
		}
		public function set isEditBlog(value:Boolean):void
		{
			_isEditBlog = value;
		}
		
		private var _isSubscribe:Boolean;
		public function get isSubscribe():Boolean
		{
			return _isSubscribe;
		}
		public function set isSubscribe(value:Boolean):void
		{
			_isSubscribe = value;
		}
		
		//----------------------------------------------------------------------------
		//
		// Filter methods
		//
		//----------------------------------------------------------------------------
		
		//filter IGetAlbumAble
		public static function getGetAlbumAbleService(service:Vector.<ServiceVO>):Vector.<ServiceVO>
		{
			return service.filter(isGetAlbumFunction);
		}
		private static function isGetAlbumFunction(element:ServiceVO, index:int, arr:Vector.<ServiceVO>):Boolean 
		{
        	return element.isGetAlbum;
    	}
    	
    	//filter IUploadPhotoAble
		public static function getPostAlbumAbleService(service:Vector.<ServiceVO>):Vector.<ServiceVO>
		{
			return service.filter(isPostAlbumFunction);
		}
		private static function isPostAlbumFunction(element:ServiceVO, index:int, arr:Vector.<ServiceVO>):Boolean 
		{
        	return element.isPostAlbum;
    	}
    	 
    	//filter IPostBlogAble 	
		public static function getPostBlogAbleService(service:Vector.<ServiceVO>):Vector.<ServiceVO>
		{
			return service.filter(isPostBlogFunction);
		}
		private static function isPostBlogFunction(element:ServiceVO, index:int, arr:Vector.<ServiceVO>):Boolean 
		{
        	return element.isPostBlog;
    	}
    	 
    	//filter IUpdateBlogAble  	
		public static function getEditBlogAbleService(service:Vector.<ServiceVO>):Vector.<ServiceVO>
		{
			return service.filter(isEditBlogFunction);
		}
		private static function isEditBlogFunction(element:ServiceVO, index:int, arr:Vector.<ServiceVO>):Boolean 
		{
        	return element.isEditBlog;
    	}
		
		//filter ISubscribeAble
		public static function getSubscribeAbleService(service:Vector.<ServiceVO>):Vector.<ServiceVO>
		{
			return service.filter(isSubscribeFunction);
		}
    	private static function isSubscribeFunction(element:ServiceVO, index:int, arr:Vector.<ServiceVO>):Boolean 
		{
        	return element.isSubscribe;
    	}
    	
    	//----------------------------------------------------------------------------
		//
		// Convert Vector to Array
		//
		//----------------------------------------------------------------------------
		
		public static function VectorToArray(v:Vector.<ServiceVO>):Array/* of ServiceVO */
		{
			var a:Array = [];
			for each(var i:ServiceVO in v)
			{
				a.push(i);
			}
			return a;
		}
		
		public static function VectorToXML(v:Vector.<ServiceVO>):XML
		{
			var xml:XML = <service></service>;
			for each(var item:ServiceVO in v)
			{
				var svo:XML = <vo name={item.name} type={item.type} link={item.link} key={item.key} secret={item.secret} 
				authhead={item.authhead} authparams={item.authparams} login_url={item.login_url} login_flag={item.login_flag} 
				login_width={item.login_width} login_height={item.login_height} endpoint={item.endpoint} 
				destination={item.destination} config={item.config} needAlert={item.needAlert} isEditBlog={item.isEditBlog} 
				isGetAlbum={item.isGetAlbum} isPostAlbum={item.isPostAlbum} isPostBlog={item.isPostBlog} isSubscribe={item.isSubscribe}/>
				xml.appendChild(svo);
			}
			return xml;
		}
		
		public static function XMLToVector(x:XML):Vector.<ServiceVO>
		{
			var dataArray:Vector.<ServiceVO> = new Vector.<ServiceVO>();
			for each(var xml:XML in x.vo)
			{
				var _data:ServiceVO = new ServiceVO();
				_data.type = int(xml.@type);
				_data.name = xml.@name;
				_data.link = xml.@link;
				_data.key = xml.@key;
				_data.secret = xml.@secret;
				_data.username = xml.@username;
				_data.password = xml.@password;
				_data.authhead = xml.@authhead;
				_data.authparams = xml.@authparams;
				_data.login_url = xml.@login_url;
				_data.login_flag = xml.@login_flag;
				_data.login_width = int(xml.@login_width);
				_data.login_height = int(xml.@login_height);
				_data.endpoint = xml.@endpoint;
				_data.destination = xml.@destination;
				_data.needAlert = xml.@needAlert;
				_data.isGetAlbum = String(xml.@isGetAlbum) == "true";
				_data.isPostAlbum = String(xml.@isPostAlbum) == "true";
				_data.isSubscribe = String(xml.@isSubscribe) == "true";
				_data.isPostBlog = String(xml.@isPostBlog) == "true";
				_data.isEditBlog = String(xml.@isEditBlog) == "true";
				_data.config = xml.@config;
				dataArray.push(_data);
			}
			return dataArray;
		}
	}
}